// detail/mwdetail/mwdetail.js
Page({

  //页面的初始数据
  data:{

  },

  //点赞
  like:function(){
    let says=this.data.say 
    if(says.islike=="true"){  
      says.islike="false"
      says.likenum=--says.likenum
      says.likeimg="/icon/like.png"
    }else{
      says.islike="true"
      says.likenum=++says.likenum
      says.likeimg="/icon/like1.png"
    }

    //更改当前动态点赞数量信息
    wx.request({
      url: `https://www.prebeauty.work:3001/api/says/${this.data.say.sayid}`,
      data:{
        likenum:this.data.say.likenum,
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'PUT',
      success:(res)=>{

      }
    })
    //更改当前动态用户点赞信息
    wx.request({
      url: `https://www.prebeauty.work:3001/api/like/${this.data.say.likeid}`,
      data:{
        sayid:this.data.say.sayid,
        userid:this.data.id,
        islike:this.data.say.islike,
        likeimg:this.data.say.likeimg
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'PUT',
      success:(res)=>{
        this.setData({
          say:says
        })
      }
    })
  },

   //关注
   attention:function(e){
    console.log(e)
    const sayid=e.currentTarget.dataset.sayid//动态id
    let attention=e.currentTarget.dataset.attention//是否关注
    const attentionid=e.currentTarget.dataset.attentionid//被点击者id

    
    let method=''
    if(attention=="已关注"){//取消关注
      method='DELETE'
      attention="关注"
      wx.request({
        url: `https://www.prebeauty.work:3001/api/attention/${this.data.id}`,
        data:{
          attentionid:attentionid
        },
        header: {
          'content-type': 'application/json'
        },
        method: method,
        success:(res)=>{
          console.log("attention click success!")
        }
      })
    }else{//关注
      method='POST'
      attention="已关注"
      wx.request({
        url: "https://www.prebeauty.work:3001/api/attention",
        data:{
          userid:this.data.id,
          attentionid:attentionid
        },
        header: {
          'content-type': 'application/json'
        },
        method: method,
        success:(res)=>{
          console.log("attention click success!")
        }
      })
    }
    
    //将本地的attention进行更新
    let says=this.data.say
    says.attention=attention

    this.setData({
      say:says
    })
    console.log(this.data.say)
  },



  //编辑评论
  setcomment:function(e){
    //编辑时间、内容
    var comment={
      commenttext:e.detail.value,
      sayid:this.data.say.sayid,
      userid:this.data.id,
      username:this.data.name,
      userimg:this.data.img
    }
    //存储当条评论
    wx.setStorageSync('comment',comment)
  },

  //发布评论
  addComment:function(){
    //获取需发布评论
    let comment=wx.getStorageSync('comment')
    //获取发布时间
    var util=require('../../pages/utils/util.js')
    comment.commenttime=util.formatTime(new Date())

    //添加评论
    wx.request({
      url: `https://www.prebeauty.work:3001/api/comment`,
      data:{
        userid:comment.userid,
        sayid:comment.sayid,  
        commenttext:comment.commenttext,
        commenttime:comment.commenttime
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success:async (res)=>{  
        comment.commentid=res.data

        await new Promise((rv,rj) => {
          wx.request({
            url: `https://www.prebeauty.work:3001/api/says/${comment.sayid}`,
            data:{
              commentnum:++this.data.say.commentnum
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'PUT',
            success:(res)=>{
              console.log(res.data)
              console.log("comment add success!")
              rv()
            },
            fail : err => {
              rv()
            }
          })
        })
      }
    })

    //本地添加评论
    let comments=this.data.commentlist
    comments.push(comment)
    this.setData({
        commentlist:comments,
        inputValue:'',
        say:this.data.say
    })

    console.log(this.data.say)
    console.log(this.data.commentlist)

  },

  //生命周期函数--监听页面加载
  onLoad: function (options) {
    //判断登录
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
          })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/index/index'
          })
          }
        }
      })
    }

    this.setData({
      id: wx.getStorageSync('userid'),
      img:wx.getStorageSync('userimg'),
      name:wx.getStorageSync('username'),
    })



    console.log(options.sayid)
    wx.request({
      url: `https://www.prebeauty.work:3001/api/says?sayid=${options.sayid}&groupid=${options.groupid}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:async (res)=>{
        console.log(res.data)
        let says=res.data[0]

        
        //图片 字符串->数组
        if(says.sayimg){
          let sayimgs=says.sayimg.slice(1,-1)
          sayimgs=sayimgs.split(',')
          
          for(let j=0;j<sayimgs.length;j++){
            sayimgs[j]=sayimgs[j].slice(1,-1)
            sayimgs[j]="https://www.prebeauty.work:3001/static/image/"+sayimgs[j]
          }
          says.sayimg=sayimgs
        }
        console.log(says.sayimg)

        //判断是否点赞 初始点赞数
        await new Promise((rv,rj)=>{
          wx.request({
            url: `https://www.prebeauty.work:3001/api/like?userid=${this.data.id}&sayid=${says.sayid}`,
            header: {
              'content-type': 'application/json'
            },
            method: 'GET',
            success:(res)=>{
              says.likeid=res.data[0].id
              says.islike=res.data[0].islike
              says.likeimg=res.data[0].likeimg
              rv()
            },
            fail : err => {
              rv()
            }
          })
        })
        

        //头像 和 用户名
        await new Promise((rv,rj) => {
          wx.request({
            url: `https://www.prebeauty.work:3001/api/userpage?userid=${says.userid}`,
            header: {
              'content-type': 'application/json'
            },
            method: 'GET',
            success:(res)=>{
              says.username=res.data[0].username
              says.userimg=res.data[0].userimg
              

              //关注
              wx.request({
                url: `https://www.prebeauty.work:3001/api/attention?userid=${this.data.id}&attentionid=${says.userid}`,
                header: {
                  'content-type': 'application/json'
                },
                method: 'GET',
                success:(res)=>{
                  console.log(res.data)
                  says.attention=res.data?"已关注":"关注"
                  rv()
                },
                fail : err => {
                  rv()
                }
              })
            },
          })
        })
        
        console.log(1)
        this.setData({
          say:says
        });
        console.log(this.data.say)
      }
    });

    wx.request({
      url: `https://www.prebeauty.work:3001/api/comment?sayid=${options.sayid}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success:async (res)=>{
        console.log(res.data)
        let comments=res.data
        for(let i=0;i<comments.length;i++){

          await new Promise((rv,rj) => {
            wx.request({
              url: `https://www.prebeauty.work:3001/api/userpage?userid=${comments[i].userid}`,
              header: {
                'content-type': 'application/json'
              },
              method: 'GET',
              success:(res)=>{
                comments[i].username=res.data[0].username
                comments[i].userimg=res.data[0].userimg
                rv()
              },
              fail : err => {
                rv()
              }
            })
          })
        }
      
        this.setData({
          commentlist:comments
        });
        console.log(this.data.commentlist)
      }
    })
    
  },

  //生命周期函数--监听页面初次渲染完成
  onReady: function () {

  },

  //生命周期函数--监听页面显示
  onShow: function () {

  },

  //生命周期函数--监听页面隐藏
  onHide: function () {

  },

  //生命周期函数--监听页面卸载
  onUnload: function () {

  },

  //页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {

  },

  //页面上拉触底事件的处理函数
  onReachBottom: function () {

  },

  //用户点击右上角分享
  onShareAppMessage: function () {

  }
  
})